
function errorImg( obj ){
    obj.src = host + "/content/images/none.png";
}

// 表单数据验证
function validate( a ){
    var flag = true;
    var form = a || $( "form input,form textarea" );
	var show = function ( obj, msg ) {
        if ( !msg ) {
            if ( obj.attr( 'placeholder' ) ) {
                msg = obj.attr( 'placeholder' ) + '!';
            }else{
                msg = '请填写该内容!';
            }
        }
		obj.focus();

        if ( typeof jBoxTip != "undefined" && jBoxTip == true ) {
            jBox.tip( msg, 'error' );
            return;
        }
		obj.parent().find( 'span.label-important' ).remove();
		obj.parent().append( '<span class="label label-important">' + msg + '</span>' );
		obj.parent().find( 'span.label-important' ).fadeOut( 5000, function () { obj.parent().find( 'span.error' ).remove(); });
	};
	var required = function( value, obj ){ if( value.length <= 0 ){ show( obj ); return false; } return true; } ;
	var username = function( value, obj ){ if( value.search( /^(\w|-){3,15}$/ ) == -1) { show( obj, '只能输入3-15个字母，数字和连字符（-）!' );return false; } return true;};
	var email = function( value ,obj ){ if( value.length > 0 && value.search( /^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)$/ ) == -1) { show( obj, '邮箱格式错误!' );return false; } return true; };
	var match = function ( value, obj ) { var p = "/" + obj.attr( 'match' ) + "/"; if ( value.search( eval( p ) ) == -1 ) { show( obj, obj.attr('tip') ); return false; } return true; };
    var isint = function( value, obj ){ if( value.search( /^([0-9]*)$/ ) == -1 ){ show( obj, '只能是数字!' ); return false; } return true; };
    var pass = function () {
        var aa = $.trim( $( 'input[name="pass"]' ).val() );
        var bb = $.trim( $( 'input[name="pass1"]' ).val() );
        if ( aa == bb ) {
            return true;
        }
        show( $( 'input[name="pass1"]' ), '2次密码不一样!' );
        return false;
    }

    form.each(function(){
		var obj = $( this );
		var value = $.trim( obj.val() );
		if ( obj.hasClass( 'required' ) ) { flag = required( value, obj );if ( !flag ) return false; }
        if ( obj.hasClass( 'username' ) && value.length > 0 ) { flag = username( value, obj );if ( !flag ) return false; }
        if ( obj.hasClass( 'pass' ) ) { flag = pass();if ( !flag ) return false; }
        if ( obj.hasClass( 'isint' ) ) { flag = isint( value, obj );if ( !flag ) return false; }
		if ( obj.hasClass( 'email' ) ) { flag = email( value, obj );if ( !flag ) return false; }
		if ( obj.attr( 'match' ) ) { flag = match( value, obj );if ( !flag ) return false; }
	});
	return flag;
}

function setButton( obj, i ){
    if ( !i ) {
        obj.attr( "disabled", true );
        obj.fadeTo( "fast", 0.5 );
    }
    if ( i ) {
        obj.fadeTo( "fast", 1 );
        obj.removeAttr( 'disabled' );
    }
}

function ajaxGET( obj, fun ) {

    var url = obj.attr( 'href' );
    var text = obj.attr( 'title' ) || obj.html();
    var referer = obj.attr( 'rel' );

    $.jBox.tip( '正在' + text + '...', 'loading', {opacity:0.2} );

    $.get( url + "&ajax=1", function ( result ) {
        if( fun ){ fun( result ); return; }
        if ( result == 'success' ) {
            window.setTimeout( function () { $.jBox.tip( text + '完成', 'success' ); }, 2000 );
            window.setTimeout( function () { if ( referer ) { window.location.href = referer; } else{ window.location.reload(); }  }, 3000 );
            return;
        }

        window.setTimeout( function(){ $.jBox.closeTip();
            if( result.length > 100 ){
                $.jBox( "<div style='padding: 10px;'>" + result + "</div>", { title:'提示', width:'auto' } )
            }else{
                $.jBox.tip( result, 'error');
            }

        }, 2000 );
    });
}

function ajaxPOST( obj, form, fun, debug ) {

    var form = form || obj.parents( 'form' );

    if ( !validate( form.find( 'input,textarea' ) ) ) { return false; }

    var tip = obj.attr( 'title' ) || obj.html();
    var referer = obj.attr( 'href' );

    $.jBox.tip( '正在' + tip + '...', 'loading', { opacity:0.2 } );

    if ( typeof (editor1) == 'object' ) {
        editor1.sync();
    }
    if ( typeof (editor2) == 'object' ) {
        editor2.sync();
    }

    var url = form.attr('action');
    var date = form.serialize();

    if ( debug ) { form.submit(); return; }

    $.post( url + "&ajax=1", date, function ( result ) {
        if( fun ){ fun( result ); return; }
        if ( result == 'success' ) {
            window.setTimeout( function(){ $.jBox.tip( tip + '成功!', 'success' );}, 2000 );
            window.setTimeout( function () { if ( referer )  window.location.href = referer; else window.location.reload(); }, 3000 );
            return;
        }
        window.setTimeout( function(){ $.jBox.closeTip();
            if( result.length > 100 ){
                $.jBox( "<div style='padding: 10px;'>" + result + "</div>", { title:'提示', width:'auto' } )
            }else{
                $.jBox.tip( result );
            }

        }, 2000 );
    });
}